'use strict';
const dayjs = require("dayjs");

module.exports = app => {
    const { INTEGER, STRING, DATE, TINYINT, DECIMAL } = app.Sequelize;
    const ExpressOrder = app.model.define(
        'ExpressOrder',
        {
            id: { type: INTEGER, primaryKey: true, autoIncrement: true },
            order_no: { type: STRING(50), allowNull: false, comment: '订单编号' },
            sender_name: { type: STRING(50), allowNull: false, comment: '发件人姓名' },
            sender_phone: { type: STRING(20), allowNull: false, comment: '发件人电话' },
            sender_address: { type: STRING(255), allowNull: false, comment: '发件人地址' },
            receiver_name: { type: STRING(50), allowNull: false, comment: '收件人姓名' },
            receiver_phone: { type: STRING(20), allowNull: false, comment: '收件人电话' },
            receiver_address: { type: STRING(255), allowNull: false, comment: '收件人地址' },
            user_id: { type: INTEGER, allowNull: true, comment: '用户ID' },
            logistics_company_id: { type: INTEGER, allowNull: false, comment: '物流公司ID' },
            warehouse_id: { type: INTEGER, allowNull: false, comment: '发送仓库ID' },
            outlet_id: { type: INTEGER, allowNull: false, comment: '接收网点ID' },
            insurance_coverage_id: { type: INTEGER, allowNull: true, comment: '保险保额ID' },
            total_weight: { type: DECIMAL(10, 2), allowNull: true, comment: '总重量（kg）' },
            total_volume: { type: DECIMAL(10, 2), allowNull: true, comment: '总体积（立方米）' },
            freight: { type: DECIMAL(10, 2), defaultValue: 0.00, comment: '运费' },
            insurance_premium: { type: DECIMAL(10, 2), defaultValue: 0.00, comment: '保险费' },
            total_amount: { type: DECIMAL(10, 2), defaultValue: 0.00, comment: '总金额' },
            order_status: { type: TINYINT(1), defaultValue: 0, comment: '订单状态：0-待发货，1-已发货，2-运输中，3-已到达，4-已签收，5-已取消' },
            payment_status: { type: TINYINT(1), defaultValue: 0, comment: '支付状态：0-未支付，1-已支付' },
            payment_time: { type: DATE, allowNull: true, comment: '支付时间' },
            delivery_time: { type: DATE, allowNull: true, comment: '发货时间' },
            receive_time: { type: DATE, allowNull: true, comment: '签收时间' },
            cancel_time: { type: DATE, allowNull: true, comment: '取消时间' },
            remark: { type: STRING(255), allowNull: true, comment: '备注' },
            create_time: {
                type: DATE,
                get() {
                    return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            },
            update_time: {
                type: DATE,
                get() {
                    return dayjs(this.getDataValue('update_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            }
        },
        {
            tableName: 'express_order',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time'
        }
    );

    ExpressOrder.associate = function() {
        app.model.ExpressOrder.belongsTo(app.model.User, { foreignKey: 'user_id', targetKey: 'id', as: 'user' });
        app.model.ExpressOrder.belongsTo(app.model.LogisticsCompany, { foreignKey: 'logistics_company_id', targetKey: 'id', as: 'logisticsCompany' });
        app.model.ExpressOrder.belongsTo(app.model.Warehouse, { foreignKey: 'warehouse_id', targetKey: 'id', as: 'warehouse' });
        app.model.ExpressOrder.belongsTo(app.model.ExpressOutlet, { foreignKey: 'outlet_id', targetKey: 'id', as: 'outlet' });
        app.model.ExpressOrder.belongsTo(app.model.InsuranceCoverage, { foreignKey: 'insurance_coverage_id', targetKey: 'id', as: 'insuranceCoverage' });
        app.model.ExpressOrder.hasMany(app.model.OrderItem, { foreignKey: 'order_id', targetKey: 'id' });
        app.model.ExpressOrder.hasMany(app.model.LogisticsTrack, { foreignKey: 'order_id', targetKey: 'id' });
    };

    return ExpressOrder;
};
